//
//  HotViewController.m
//  Tour
//
//  Created by dllo on 15/10/22.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HotViewController.h"
#import "AdvanceEdit.h"
#import "ZGSHotModel.h"
#import "ZGSHotModelTwo.h"
#import "ZGSHotCell.h"
#import "ZGSHotDetailsViewController.h"
#import "ZGSSiteViewController.h"
@interface HotViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)MBProgressHUD *HUD;
@property (nonatomic, retain)NSMutableArray *arrEmwnts;

//@property (nonatomic,)
@end

@implementation HotViewController

- (void)dealloc{
    [_tableView release];
    [_arr release];
    [_HUD release];
    [_arrEmwnts release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
}
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.hidesBarsOnSwipe = NO;
}

- (void)creatView{
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"推辞";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 100) style:UITableViewStylePlain];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    [self.tableView registerClass:[ZGSHotCell class] forCellReuseIdentifier:@"tableReuse"];
    
    
    self.arr = [NSMutableArray array];
    self.tableView.rowHeight = self.view.frame.size.height / 4;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载";
    self.HUD.dimBackground = YES;

    [self creatData];
    
    
}

- (void)creatData{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://api.breadtrip.com/destination/v3/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
//        ZGSHotModel *hot = [[ZGSHotModel alloc] init];
        self.arrEmwnts = [NSMutableArray array];
        self.arrEmwnts = [ZGSHotModel baseModelArr:dic[@"elements"]];
        
//        for (NSDictionary *dic1 in dic[@"elements"]) {
//            self.arr = [ZGSHotModelTwo baseModelArr:dic1[@"data"]];
//        }
//        NSLog(@"%@", self.arr);
        [self.tableView reloadData];
        self.HUD.hidden = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrEmwnts[section] dataa].count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [self.arrEmwnts[section] title];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGSHotCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableReuse" forIndexPath:indexPath];
    NSString *str = [NSString stringWithFormat:@"%@", [self.arrEmwnts[indexPath.section] dataa][indexPath.row][@"visited_count"]];
    NSString *strTwo = [NSString stringWithFormat:@"有%@人去过", str];
    NSString *strLike = [NSString stringWithFormat:@"%@", [self.arrEmwnts[indexPath.section] dataa][indexPath.row][@"wish_to_go_count"]];
    NSString *strLikeTmp = [NSString stringWithFormat:@"有%@人喜欢", strLike];
    cell.labelLike.text = strLikeTmp;
    cell.labelName.text =[NSString stringWithFormat:@"%@", [self.arrEmwnts[indexPath.section] dataa][indexPath.row][@"name"]];
    cell.labelGo.text =strTwo;
    [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:[self.arrEmwnts[indexPath.section] dataa][indexPath.row][@"cover_route_map_cover"]] placeholderImage:[UIImage imageNamed:@"10.jpeg"]];
//    cell.leftImage sd_setImageWithURL:<#(NSURL *)#> placeholderImage:<#(UIImage *)#>
    
;
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrEmwnts.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGSHotDetailsViewController *zgsHotVC = [[ZGSHotDetailsViewController alloc] init];
    
    if (indexPath.section == 0) {
        zgsHotVC.titleHead = @"详情";
        zgsHotVC.name = [self.arrEmwnts[0] dataa][indexPath.row][@"name"];
        zgsHotVC.introduce = [self.arrEmwnts[0] dataa][indexPath.row][@"recommended_reason"];
        zgsHotVC.outline = [self.arrEmwnts[0] dataa][indexPath.row][@"description"];
        zgsHotVC.address = [self.arrEmwnts[0] dataa][indexPath.row][@"address"];
        zgsHotVC.address = [self.arrEmwnts[0] dataa][indexPath.row][@"address"];
        zgsHotVC.goWay = [self.arrEmwnts[0] dataa][indexPath.row][@"arrival_type"];
        zgsHotVC.openTime = [self.arrEmwnts[0] dataa][indexPath.row][@"opening_time"];
        zgsHotVC.phone = [self.arrEmwnts[0] dataa][indexPath.row][@"tel"];
        zgsHotVC.imageHead = [self.arrEmwnts[0] dataa][indexPath.row][@"cover_route_map_cover"];
        zgsHotVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:zgsHotVC animated:YES];

        
    }else{
        ZGSSiteViewController *siteVC = [[ZGSSiteViewController alloc] init];
        siteVC.cityId = [self.arrEmwnts[indexPath.section] dataa][indexPath.row][@"id"];
        siteVC.photo = [self.arrEmwnts[indexPath.section] dataa][indexPath.row][@"cover_route_map_cover"];
        siteVC.name = [self.arrEmwnts[indexPath.section] dataa][indexPath.row][@"name"];
        NSString *strType = [NSString stringWithFormat:@"%@", [self.arrEmwnts[indexPath.section] dataa][indexPath.row][@"type"]];
        siteVC.type = strType;
        siteVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:siteVC animated:YES];

        
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
